package com.soviet.boot.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.soviet.boot.business.system.vo.SysUserVo;
import com.soviet.boot.common.utils.ClassScaner;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @Author Dave
 * @Date 2019/8/19
 * @Description
 */
@EnableTransactionManagement
@Configuration
@MapperScan("com.soviet.boot.business.*.mapper")
public class MybatisPlusConfig {

    /**
     * 分页插件
     *
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MARIADB));
        return interceptor;
    }

    /**
     * 缓存vo的tableInfo
     *
     * @return
     */
    @Bean("voTableInfo")
    public void cacheVoTableInfo() {
        ClassScaner.scan("com.soviet.boot.business.system.vo", null)
                .forEach(clazz -> {
                    TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), clazz);
                });
    }

}
